#!/bin/sh
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"

if [ -z "$LMD_APPS_DIR" ]; then
   echo "Error: Env LMD_APPS_DIR is not defined."
   exit 1
else 
   echo "Env LMD_APPS_DIR: "$LMD_APPS_DIR
fi

export CURRENT_APP_PATH="${LMD_APPS_DIR}/chatbox-ai"
mkdir -p "$CURRENT_APP_PATH"
if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist, please retry. "$CURRENT_APP_PATH
    exit 1
fi

source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/chatbox-ai/env_read.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/download_utils.sh"

function install_for_mac {
   hardware_type=$(uname -m)
   if [[ "$hardware_type" == "arm64" ]]; then
    echo "Apple Silicon (ARM) M-series chips"
    INSTALLER_URL=$_MAC_ARM64_DOWNLOAD_URL
    _MAC_INSTALLER_FILE_NAME=$_MAC_ARM64_INSTALLER_FILE_NAME
   elif [[ "$hardware_type" == "x86_64" ]]; then
    echo "Intel (x64) chips"
    INSTALLER_URL=$_MAC_X64_DOWNLOAD_URL
    _MAC_INSTALLER_FILE_NAME=$_MAC_X64_INSTALLER_FILE_NAME
   fi

   echo "lmdprogress:18"

   INSTALLER_FILE_PATH=$CURRENT_APP_PATH/${_MAC_INSTALLER_FILE_NAME}
   echo "start download: "$INSTALLER_URL
   echo "INSTALLER_FILE_PATH" $INSTALLER_FILE_PATH
   download_with_resume "$INSTALLER_FILE_PATH" $INSTALLER_URL
   # curl -L -C - --retry 5 -o "$INSTALLER_FILE_PATH" $INSTALLER_URL
   if [ $? -eq 0 ]; then
       echo "downloaded."
   else
       echo "download error."
       exit 1
   fi
   echo "lmdprogress:50"
   
   MOUNT_APP_PATH="/Volumes/chatbox-ai"
   # mount .dmg file
   hdiutil attach "$INSTALLER_FILE_PATH" -mountpoint $MOUNT_APP_PATH -nobrowse
   
   if [ $? -eq 0 ]; then
      echo "DMG file has been mounted successfully."
   else
      echo "Failed to mount DMG file."
   fi
    echo "lmdprogress:70"
    
    if [[ "$_MAC_INSTALL_TARGET_FILE_NAME" =~ \.app$ ]] && [[ ${#_MAC_INSTALL_TARGET_FILE_NAME} -gt 5 ]]; then
        echo "$_MAC_INSTALL_TARGET_FILE_NAME is valid"
    else
       echo "$_MAC_INSTALL_TARGET_FILE_NAME is not a valid .app name"
       exit 1
    fi
    echo "copy to $_MAC_INSTALL_PATH"
    osascript <<EOF
    do shell script "cd \"$_MAC_INSTALL_PATH\" && sudo rm -rf \"$_MAC_INSTALL_TARGET_FILE_NAME\" && sudo ditto \"$MOUNT_APP_PATH/$_MAC_INSTALL_TARGET_FILE_NAME\" \"$_MAC_INSTALL_PATH/$_MAC_INSTALL_TARGET_FILE_NAME\"" with administrator privileges
EOF
    exit_code=$?
    if [ $exit_code -ne 0 ]; then
        echo "exit_code: "$exit_code
        exit $exit_code
    fi

    echo "lmdprogress:75"
    echo "detach image"
    hdiutil detach $MOUNT_APP_PATH
    
    echo "lmdprogress:90"
}

function install_for_win {
   cpu_arch=$(powershell -NoProfile -Command "([System.Environment]::GetEnvironmentVariable('PROCESSOR_ARCHITECTURE'))")
   if [[ "$cpu_arch" == "AMD64" ]]; then
      echo "x64 CPU"
      INSTALLER_URL=$_WINDOWS_X64_DOWNLOAD_URL
      INSTALLER_FILE_PATH="$CURRENT_APP_PATH/${_WINDOWS_X64_INSTALLER_FILE_NAME}"
   else
      echo "do not support: $cpu_arch"
      exit 1
   fi
   echo "start download: "$INSTALLER_URL
   echo "lmdprogress:30"
   download_with_resume "$INSTALLER_FILE_PATH" $INSTALLER_URL
   # curl -L --ssl-no-revoke -C - --retry 5 -o "$INSTALLER_FILE_PATH" $INSTALLER_URL
   echo "lmdprogress:80"
   echo "downloaded."
   "$INSTALLER_FILE_PATH"
}

echo "lmdprogress:15"
if [ -f /etc/os-release ]; then
   echo "Linux system."
   echo "Not supported."
   exit 1
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   install_for_mac
elif current_os_is_windows; then
   echo "Windows system."
   install_for_win
else
   echo "Unknown OS"
fi


echo "lmdprogress:100"




